package org.kpli.jak.net.second;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class MultiThreadChatServer {

	static Socket clientSocket = null;
	static ServerSocket serverSocket = null;

	// Chat pada server hanya memberikan izin pada client 10
	static ClientThread t[] = new ClientThread[10];

	public static void main(String args[]) {

		int port_number = 2211;

		if (args.length < 1) {
			System.out.println("Usage: java  MultiThreadChatServer \n"
					+ "Port yang sedang digunakan=" + port_number);
		} else {
			port_number = Integer.valueOf(args[0]).intValue();
		}

		try {
			serverSocket = new ServerSocket(port_number);
		} catch (IOException e) {
			System.out.println(e);
		}

		while (true) {
			try {
				clientSocket = serverSocket.accept();
				for (int i = 0; i <= 9; i++) {
					if (t[i] == null) {
						(t[i] = new ClientThread(clientSocket, t)).start();
						break;
					}
				}
			} catch (IOException e) {
				System.out.println(e);
			}
		}
	}
}
